(function() {
    'use strict';

    angular
        .module('milkbeeApp')
        .controller('SweepstakeDialogController', SweepstakeDialogController);

    SweepstakeDialogController.$inject = ['$timeout', '$scope', '$stateParams', '$uibModalInstance', 'entity', 'Sweepstake'];

    function SweepstakeDialogController ($timeout, $scope, $stateParams, $uibModalInstance, entity, Sweepstake) {
        var vm = this;

        vm.sweepstake = entity;
        vm.clear = clear;
        vm.datePickerOpenStatus = {};
        vm.openCalendar = openCalendar;
        vm.save = save;
        vm.datepickerOptionsEnd= {
            	minDate:new Date(),
            	maxDate: null
        }
        vm.datepickerOptionsStart= {
            	minDate:new Date(),
            	maxDate: null
            }
        $timeout(function (){
            angular.element('.form-group:eq(1)>input').focus();
        });

        function clear () {
            $uibModalInstance.dismiss('cancel');
        }

        function save () {
            vm.isSaving = true;
            if (vm.sweepstake.id !== null) {
                Sweepstake.update(vm.sweepstake, onSaveSuccess, onSaveError);
            } else {
                Sweepstake.save(vm.sweepstake, onSaveSuccess, onSaveError);
            }
        }

        function onSaveSuccess (result) {
            $scope.$emit('milkbeeApp:sweepstakeUpdate', result);
            $uibModalInstance.close(result);
            vm.isSaving = false;
        }

        function onSaveError () {
            vm.isSaving = false;
        }

        vm.datePickerOpenStatus.startAt = false;
        vm.datePickerOpenStatus.endAt = false;
        vm.datePickerOpenStatus.createdAt = false;

        function openCalendar (date) {
            vm.datePickerOpenStatus[date] = true;
        }
    }
})();
